package vooga.xml;


/**
 * This class is the base class for those that can handle specific 
 * XML nodes in a particular way.
 * 
 * @author David Winslow
 */
public abstract class XMLHandler
{
    private XMLDocument myDocument;


    public abstract Object handleNode (org.w3c.dom.Node n, Object context) throws XMLException;


    /**
     * Gets all attribute nodes associated with this node
     * 
     * @param node the Node containing the information
     */
    public static java.util.List<org.w3c.dom.Node> getAttributes (org.w3c.dom.Node node)
    {
        java.util.List<org.w3c.dom.Node> results = new java.util.ArrayList<org.w3c.dom.Node>();
        org.w3c.dom.NamedNodeMap attributes = node.getAttributes();
        for (int k = 0; k < attributes.getLength(); k++)
        {
            results.add(attributes.item(k));
        }
        return results;
    }

    /**
     * Gets the value associated with the tag in the XML file (ie, format is
     * 
     * <pre>
     *       &lt;nodename tag=&quot;value&quot;/&gt; 
     * </pre>). 
     * 
     * This is a one-liner either way but I thought I'd make it a function 
     * because it's easier to remember this way. It also allows me to
     * do some graceful error handling rather than throwing an exception when
     * the tag isn't found.
     * 
     * @param node the Node containing the information
     * @param tag a String containing the name associated with the information
     * @return a String containing the requested information
     */
    public static String getAttribute (org.w3c.dom.Node node, String tag)
    {
        org.w3c.dom.Node item = node.getAttributes().getNamedItem(tag);
        if (item == null) return null;
        else              return item.getNodeValue();
    }

    /**
     * Gets a list of subNodes that have the given name (tags of the form
     * 
     * <pre>
     *      &lt;name&gt; 
     * </pre>.)
     * 
     * @param node the Node containing the information
     * @param name the name of the desired node type
     * @return a List containing zero or more Nodes with the desired name
     */
    public static java.util.List<org.w3c.dom.Node> getSubNodes (org.w3c.dom.Node node, String name)
    {
        java.util.List<org.w3c.dom.Node> results = new java.util.ArrayList<org.w3c.dom.Node>();

        org.w3c.dom.NodeList children = node.getChildNodes();
        for (int k = 0; k < children.getLength(); k++)
        {
            org.w3c.dom.Node current = children.item(k);
            if (current.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && 
                (name == null || name.equals(current.getNodeName().toLowerCase())))
            {
                results.add(current);
            }
        }

        return results;
    }

    public static java.util.List<org.w3c.dom.Node> getSubNodes (org.w3c.dom.Node node)
    {
        return getSubNodes(node, null);
    }


    protected void setDocument (XMLDocument document)
    {
        myDocument = document;
    }

    protected XMLDocument getDocument ()
    {
        return myDocument;
    }
}
